package com.pocket.insight.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.mybatis.provider.Entity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author zhaozhile
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "数据连接实体")
@Entity.Table(value = "connection")
public class Connection implements Serializable {

    @Entity.Column(id = true)
    @Schema(name = "主键")
    private Long              id;

    @Schema(name = "标题")
    private String            title;

    @Schema(name = "连接配置")
    private ConnectionOptions options;

    @Schema(name = "创建者")
    private Long              createdBy;

    @Schema(name = "创建时间")
    private LocalDateTime     createdAt;

    @Schema(name = "更新者")
    private Long              updatedBy;

    @Schema(name = "更新时间")
    private LocalDateTime     updatedAt;

    @Schema(name = "是否删除")
    private Boolean           isDelete;

    @Schema(name = "版本")
    private Long              version;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(name = "数据连接配置")
    public static class ConnectionOptions implements Serializable {
        private static final long serialVersionUID = 560311843637778323L;

        @Schema(name = "type", description = "连接类型")
        private String  type;

        @Schema(name = "realType", description = "真实连接类型 TODO")
        private String  realType;

        @Schema(name = "url", description = "连接地址")
        private String  url;

        @Schema(name = "host", description = "主机")
        private String  host;

        @Schema(name = "port", description = "端口")
        private Integer port;

        @Schema(name = "username", description = "用户名")
        private String  username;

        @Schema(name = "password", description = "密码")
        private String  password;

        @Schema(name = "maxConnNum", description = "最大数据连接数")
        private Integer maxConnNum = Integer.valueOf(10);

        @Schema(name = "params", description = "连接参数")
        private String  params;

        private String jdbcVersion;

        private Boolean mysql8OnAzure;

        private String encoding;

        private MetaConfig metaConfig;

        private String readIsolation;

        // 获取数据连接下表相关配置属性
        private Boolean onlyShowSpecifiedSchema;
        private String database;

        // postgresql
        private String ssl;
        private String sslMode;
        private Config config = new Config();


        public String fetchRealType() {
            if (StringUtils.isNotBlank(getRealType())) {
                return getRealType();
            }
            return getType();
        }

        @Data
        @JsonIgnoreProperties(ignoreUnknown = true)
        public static class Config implements Serializable {
            private static final long serialVersionUID = 1;

            private Boolean jsonAvailable;
            private Boolean transactionReadOnly;

            public boolean equals(Object object) {
                if (this == object) return true;
                if (!(object instanceof Config)) return false;
                Config config = (Config)object;
                return new EqualsBuilder().append(getJsonAvailable(), config.getJsonAvailable()).isEquals();
            }

            public int hashCode() {
                return new HashCodeBuilder(17, 37).append(getJsonAvailable()).toHashCode();
            }
        }
    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class MetaConfig implements Serializable {
        private Long dbMajorVersion;
        private Long dbMinorVersion;
        private String dbProductName;
        private String dbProductVersion;
    }

}
